/**
 * Created by guojanyu on 2016/3/1.
 */
myApp.config(function ($routeProvider) {
    $routeProvider.when('/top/view', {
        templateUrl: 'top/view',
        controller: 'topListController'
    })
});
/*ajax获取后台表单数据*/
myApp.service('topListService', function ($http) {
    var doRequest = function () {
        return $http.get('top/list');
    }
    return {
       topList: function () {
            return doRequest();
        }
    };
});
myApp.controller('topListController', function ($scope, $location, $http,topListService,Notify) {
    /*获取表单数据*/
    topListService.topList().success(function (response) {
        $scope.tops = response;
    });
    /*删除top*/
    $scope.deleteTop = function (id,name) {
        if (confirm('确定要删除"' + name + '"吗?')) {
            $http.get('top/delete/' + id).success(function () {
                Notify.success('删除成功');
                /*重新获取页面数据目的刷新refresh*/
                topListService.topList().success(function (response) {
                    $scope.tops = response;
                });
            }).error(function () {
                Notify.error('删除失败');
            });
        }
    };
    /*增加top*/
    $scope.addTop=function(){
        $scope.top={};
    };
    /*更新top*/
    $scope.updateTop=function(data){
        $scope.top =angular.copy(data);
    };
    $scope.saveTop = function () {
        $http.post('top/edit', $scope.top).success(function () {
            Notify.success("成功");
            /*重新获取页面数据目的刷新refresh*/
            topListService.topList().success(function (response) {
                $scope.tops = response;
            });
        }).error(function () {
            Notify.error('失败');
        });
    };
});
